//
//  RoomInfo.m
//  Maze1
//
//  Created by Lin Tian on 9/26/11.
//  Copyright 2011 tina. All rights reserved.
//

#import "RoomInfo.h"
#import "MapLayer.h"
#import "Player.h"
#import "RoomInfoBackground.h"
#import "OptionLayer.h"
#import "SimpleAudioEngine.h"

#define BACKGROUND -2

#define PLAYBUTTON 0
#define QUITBUTTON 1
#define OPTIONBUTTON 2

#define OPTIONLAYER 100

@implementation RoomInfo

@synthesize tempLabel;
@synthesize room = _room;


+(CCScene*) sceneWithRoom:(Room*)aRoom
{
	CCScene *myScene = [CCScene node];
	
	RoomInfo *roomInfo = [[RoomInfo alloc] initWithRoom:aRoom];
    
   	[myScene addChild:roomInfo z:0 tag:0];
    [roomInfo release];
	
    RoomInfoBackground *bg = [RoomInfoBackground node];
    [myScene addChild:bg z:BACKGROUND];
    
	return myScene;
}
-(void)updatePlayer:(ccTime)dt{
   //add player;
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    for (Player* player in self.room.otherPlayersList){
        CCSprite* player = [CCSprite spriteWithFile:@"Icon.png"];
        player.position = ccp(winSize.width/2 + player.contentSize.width*2, winSize.height*0.4);
        [self addChild:player];
    }

   }

-(id)initWithRoom:(Room*)aRoom
{
	if ((self = [super init])) {
        
       self.room = aRoom;
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        [self setupMenu];
        //code here is for debug use. delete it when real login page is on
        int roomID = 0;
        roomID = self.room.roomId;
        NSString *idlabel = [NSString stringWithFormat:@"ROOM#:%d", roomID];
        tempLabel = [CCLabelTTF labelWithString:idlabel 
                                       fontName:@"Marker Felt"
                                       fontSize:40];
        
        tempLabel.position = ccp(winSize.width/2, winSize.height*0.5);
        [self addChild:tempLabel z:2];
       //room player
       //builder
        CCSprite* builder = aRoom.owner.playerSprite;
        if(builder == nil){
            builder = [CCSprite spriteWithFile:@"Icon.png"];
        }
        builder.position = ccp(winSize.width/2 - builder.contentSize.width*2, winSize.height*0.4);
        
        CCSprite* bready = [CCSprite spriteWithFile:@"ready.png"];
        bready.position = ccp(-30,50);
        [builder addChild:bready z:1];
        [self addChild:builder];
        
        //add other player
        //int count = [self.room.otherPlayersList count];
        for (Player* player in self.room.otherPlayersList){
            CCSprite* player = [CCSprite spriteWithFile:@"Icon.png"];
            player.position = ccp(winSize.width/2 + player.contentSize.width*2, winSize.height*0.4);
            [self addChild:player];
        }
        [self schedule:@selector(updatePlayer:) interval:1]; 
        
	}
	return self;
}

-(void)dealloc
{
    [_room release];
    _room = nil;
    
    [tempLable release];
    tempLabel = nil;
    
	[super dealloc];
}

-(void)setupMenu
{
	CCMenuItemImage * menuItem1 = [CCMenuItemImage itemFromNormalImage:@"StartButton.png"
														 selectedImage: @"StartPressed.png"
																target:self
															  selector:@selector(transit:)];
	menuItem1.tag = PLAYBUTTON;
	CCMenuItemImage * menuItem2 = [CCMenuItemImage itemFromNormalImage:@"OptionButton.png"
														 selectedImage: @"OptionPressed.png"
																target:self
															  selector:@selector(transit:)];
	
	menuItem2.tag = OPTIONBUTTON;
    
    CCMenuItemImage *menuItem3 = [CCMenuItemImage itemFromNormalImage:@"QuitButton.png"
                                                               selectedImage: @"QuitPressed  .png"
                                                                      target:self
                                                                    selector:@selector(transit:)];
    menuItem3.tag = QUITBUTTON;
	// Create a menu and add your menu items to it
	CCMenu *myMenu = [CCMenu menuWithItems:menuItem1, menuItem2, menuItem3, nil];
	
	myMenu.position = ccp(500, 100);
	// Arrange the menu items vertically
	[myMenu alignItemsHorizontally];
	
	// add the menu to your scene
	[self addChild:myMenu z:1 tag: 999];
}

-(void)transit:(CCMenuItem*)sender
{
	//connect to server
	PackageParser *aParser = [PackageParser sharedPackageParser];
	if(sender.tag == PLAYBUTTON){
        [[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
        aParser.state = PlayerStateStartingGame;
    }
    else if(sender.tag == QUITBUTTON){
        aParser.state = PlayerStateInHall;
        //transit to RoomList Layer
    }
    else if(sender.tag == OPTIONBUTTON){
        
        if([self getChildByTag:OPTIONLAYER] == nil){
        OptionLayer *ol = [OptionLayer node];
        
            [self addChild:ol z:5 tag:OPTIONLAYER];
        }
    }
	//else show connection error page
    aParser = nil;
}

@end
